<?php

// https://www.google.com/accounts/o8/ud?openid.ns=http://specs.openid.net/auth/2.0&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.return_to=http://mydomain.com/Gmail-Login/success.php&openid.realm=http://mydomain.com&openid.assoc_handle=AOQobUeu_iPKBY7PXz6ZfKjlEZSZM--mbt0ai6P9THVt-p5i3U-gnknn&openid.mode=checkid_setup&openid.ns.ext1=http://openid.net/srv/ax/1.0&openid.ext1.mode=fetch_request&openid.ext1.type.email=http://schema.openid.net/contact/email&openid.ext1.type.firstname=http://axschema.org/namePerson/first&openid.ext1.type.lastname=http://axschema.org/namePerson/last&openid.ext1.type.country=http://axschema.org/contact/country/home&openid.ext1.required=email,firstname,lastname,country

class Google
{
	private $_callbackUrl = null;

	public function __construct($callbackUrl = null, $aProfileFields = array())
	{
		$this->_callbackUrl = $callbackUrl;
	}

	/**
	 * @return void
	 */
	public function _getRequestToken()
	{
		unset($_SESSION['google']['profile_info']);
		$googleLogin = GoogleOpenID::createRequest($this->_callbackUrl, null, true);
		$googleLogin->redirect();
	}

	/**
	 * @return mixed|null|stdClass
	 */
	public function getProfileInfo()
	{
		if (!empty($_SESSION['google']['profile_info'])) {
			return unserialize($_SESSION['google']['profile_info']);
		}
		else {
			$googleLogin = GoogleOpenID::getResponse();

			if($googleLogin->success()) {
				$gUserID = $googleLogin->identity();
				$gUserEmail = $googleLogin->email();
				if ($gUserEmail && $gUserID) {
					$oProfile = new stdClass();
					$oProfile->id = $gUserID;
					$oProfile->email = $gUserEmail;
					$oProfile->firstName = $googleLogin->getFirstname();
					$oProfile->lastName = $googleLogin->getLastname();
					$oProfile->country = $googleLogin->getCountry();

					$_SESSION['google']['profile_info'] = serialize($oProfile);
					return $oProfile;
				}
			}
		}
		return null;
	}



	public function revoke()
	{
		/**
		 * https://www.google.com/accounts/Logout
		 */
	}


}



